package com.sanji.sjmallmodule.goods;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.astuetz.PagerSlidingTabStrip;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallmanager.activity.ActivityManager;
import com.sanji.sjmallmanager.datamanager.SelectDataManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 * 商品配件
 *
 * */
public class GoodsPartsActivity extends FragmentActivity implements View.OnClickListener {
    @BindView(R.id.common_back_img)
    ImageView commonBackImg;
    @BindView(R.id.common_left_tv)
    TextView commonLeftTv;
    @BindView(R.id.common_right_img)
    ImageView commonRightImg;
    @BindView(R.id.goods_parts_viewPager)
    ViewPager goodsPartsViewPager;
    @BindView(R.id.parts_tabs)
    PagerSlidingTabStrip partsTabs;
    private List<GoodsTypeBean> listType;
    private List<Fragment> listFragment;
    private GoodsPartsFramentAdapter framentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_parts);
        ButterKnife.bind(this);
        ActivityManager.getInstance().addActivity(this);
        initView();
        getData();
    }

    public void initView() {
        commonLeftTv.setText(getString(R.string.professional_accessories));
        commonBackImg.setOnClickListener(this);
        commonRightImg.setImageResource(R.mipmap.goods_detail);
        listType = new ArrayList<>();
        listFragment = new ArrayList<>();
    }

    /**
     * 给fragment传递数据
     */
    public void getData() {
        //模拟数据类型
        listType = SelectDataManager.getGoodsTypeCatogrey();
        for (int i = 0; i < listType.size(); i++) {
            Fragment fragment = new GoodsPartsFrament();
            //bundle之前传递的数据
            Bundle bundle = new Bundle();
            bundle.putInt("categoryId", listType.get(i).getId());
            fragment.setArguments(bundle);
            listFragment.add(fragment);
        }
        framentAdapter = new GoodsPartsFramentAdapter(getSupportFragmentManager(), listFragment, listType);
        goodsPartsViewPager.setAdapter(framentAdapter);
        //将viewPager绑定于tabLayout
        partsTabs.setViewPager(goodsPartsViewPager);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back_img:
                finish();
                break;
        }

    }
}
